<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="10" :lg="11" :md="12" :sm="24">
            <a-form-item :label="$t('开始时间')">
              <a-date-picker
                :show-time="{ defaultValue: moment('00:00:00', 'HH:mm:ss') }"
                valueFormat="YYYY-MM-DD HH:mm:ss"
                dateFormat="YYYY-MM-DD HH:mm:ss"
                :placeholder="$t('请选择开始时间')"
                class="query-group-cust"
                v-model="queryParam.beginTime_begin"
                style="min-width: calc(50% - 10px)"
              ></a-date-picker>
              <span class="query-group-split-cust"></span>
              <a-date-picker
                :show-time="{ defaultValue: moment('23:59:59', 'HH:mm:ss') }"
                valueFormat="YYYY-MM-DD HH:mm:ss"
                dateFormat="YYYY-MM-DD HH:mm:ss"
                :placeholder="$t('请选择结束时间')"
                class="query-group-cust"
                v-model="queryParam.beginTime_end"
                style="min-width: calc(50% - 10px)"
              ></a-date-picker>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item :label="$t('游乐项目')">
              <j-dict-select-tag
                :placeholder="$t('请选择游乐项目')"
                v-model="queryParam.gameId"
                dictCode="bis_game,name,id,consume_type=3"
              />
            </a-form-item>
          </a-col>
          <template v-if="toggleSearchStatus">
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item :label="$t('开始设备')">
                <j-dict-select-tag
                  :placeholder="$t('请选择设备')"
                  v-model="queryParam.deviceId"
                  dictCode="bis_device_info,name,id"
                />
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item :label="$t('会员信息')">
                <a-input :placeholder="$t('请输入手机号/卡号/姓名')" v-model="queryParam.memberInfo"></a-input>
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item :label="$t('结算状态')">
                <j-dict-select-tag
                  :placeholder="$t('请选择结算状态')"
                  v-model="queryParam.settleState"
                  dictCode="bis_game_timer_record.settle_state"
                />
              </a-form-item>
            </a-col>
            <a-col :xl="10" :lg="11" :md="12" :sm="24">
              <a-form-item :label="$t('结束时间')">
                <a-date-picker
                  :show-time="{ defaultValue: moment('00:00:00', 'HH:mm:ss') }"
                  valueFormat="YYYY-MM-DD HH:mm:ss"
                  dateFormat="YYYY-MM-DD HH:mm:ss"
                  :placeholder="$t('请选择开始时间')"
                  class="query-group-cust"
                  v-model="queryParam.endTime_begin"
                  style="min-width: calc(50% - 10px)"
                ></a-date-picker>
                <span class="query-group-split-cust"></span>
                <a-date-picker
                  :show-time="{ defaultValue: moment('23:59:59', 'HH:mm:ss') }"
                  valueFormat="YYYY-MM-DD HH:mm:ss"
                  dateFormat="YYYY-MM-DD HH:mm:ss"
                  :placeholder="$t('请选择结束时间')"
                  class="query-group-cust"
                  v-model="queryParam.endTime_end"
                  style="min-width: calc(50% - 10px)"
                ></a-date-picker>
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item :label="$t('结算方式')">
                <j-dict-select-tag
                  :placeholder="$t('请选择结算方式')"
                  v-model="queryParam.settleType"
                  dictCode="bis_game_timer_record.settle_type"
                />
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item :label="$t('结算设备')">
                <j-dict-select-tag
                  :placeholder="$t('请选择结算设备')"
                  v-model="queryParam.settleDeviceId"
                  dictCode="bis_device_info,name,id"
                />
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item :label="$t('操作员')">
                <j-dict-select-tag
                  :placeholder="$t('请选择操作员')"
                  v-model="queryParam.updateBy"
                  dictCode="sys_user,realname,realname"
                />
              </a-form-item>
            </a-col>
          </template>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <span style="float: left; overflow: hidden" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">{{ $t('查询') }}</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">{{
                $t('重置')
              }}</a-button>
              <a @click="handleToggleSearch" style="margin-left: 8px">
                {{ toggleSearchStatus ? $t('收起') : $t('展开') }}
                <a-icon :type="toggleSearchStatus ? 'up' : 'down'" />
              </a>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button v-has="'BisGameTimerRecordController:export'" type="primary" icon="download" :loading="exloading" @click="handleExportXls($t('计时消费明细'))">{{
        $t('导出')
      }}</a-button>
    </div>

    <!-- table区域-begin -->
    <div>
      <a-table
        ref="table"
        size="middle"
        :scroll="{ x: true }"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :customHeaderRow="customHeaderRow"
        :customRow="customRow"
        class="j-table-force-nowrap"
        @change="handleTableChange"
      >
        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px; font-style: italic">{{ $t('无图片') }}</span>
          <img
            v-else
            :src="getImgView(text)"
            height="25px"
            alt=""
            style="max-width: 80px; font-size: 12px; font-style: italic"
          />
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px; font-style: italic">{{ $t('无文件') }}</span>
          <a-button v-else :ghost="true" type="primary" icon="download" size="small" @click="downloadFile(text)">
            {{ $t('下载') }}
          </a-button>
        </template>

        <span slot="action" slot-scope="text, record">
          <span @click.stop.prevent="handleParentClick">
            <a-popconfirm v-has="'BisGameTimerRecordController:settlement'" :title="$t('您确定要对该订单进行结算吗？')" @confirm="() => handleCheckout(record)">
              <a v-if="record.settleState == 0">{{ $t('结算') }}</a>
            </a-popconfirm>
          </span>
        </span>
      </a-table>
    </div>

    <bis-game-timer-record-modal ref="modalForm" @ok="modalFormOk"></bis-game-timer-record-modal>
  </a-card>
</template>

<script>
import moment from 'moment'
import { getAction, postAction } from '@/api/manage'
import '@/assets/less/TableExpand.less'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import BisGameTimerRecordModal from './modules/BisGameTimerRecordModal'
import { filterMultiDictText } from '@/components/dict/JDictSelectUtil'

export default {
  name: 'BisGameTimerRecordList',
  mixins: [JeecgListMixin],
  components: {
    BisGameTimerRecordModal,
  },
  data() {
    return {
      moment,
      description: '计时消费明细管理页面',
      // 表头
      columns: [
        {
          title: this.$t('开始时间'),
          align: 'center',
          dataIndex: 'beginTime',
        },
        {
          title: this.$t('游乐项目'),
          align: 'center',
          dataIndex: 'gameId_dictText',
        },
        {
          title: this.$t('开始设备编码'),
          align: 'center',
          dataIndex: 'deviceId',
        },
        {
          title: this.$t('开始设备名称'),
          align: 'center',
          dataIndex: 'deviceId_dictText',
        },
        {
          title: this.$t('会员手机号'),
          align: 'center',
          dataIndex: 'memberPhone',
        },
        {
          title: this.$t('卡号'),
          align: 'center',
          dataIndex: 'chipNo',
        },
        {
          title: this.$t('姓名'),
          align: 'center',
          dataIndex: 'memberName',
        },
        {
          title: this.$t('结算状态'),
          align: 'center',
          dataIndex: 'settleState_dictText',
        },
        {
          title: this.$t('结算方式'),
          align: 'center',
          dataIndex: 'settleType_dictText',
        },
        {
          title: this.$t('结束时间'),
          align: 'center',
          dataIndex: 'endTime',
        },
        {
          title: this.$t('消费时长（分钟）'),
          align: 'right',
          dataIndex: 'useTime',
          customRender: (text, r) => {
            return text.toFixed(0);
          }
        },
        {
          title: this.$t('消费币值'),
          align: 'right',
          dataIndex: 'coin',
          customRender: (text, r) => {
            return text.toFixed(2);
          }
        },
        {
          title: this.$t('结算设备编码'),
          align: 'center',
          dataIndex: 'settleDeviceId',
        },
        {
          title: this.$t('结算设备名称'),
          align: 'center',
          dataIndex: 'settleDeviceId_dictText',
        },
        {
          title: this.$t('操作员'),
          align: 'center',
          dataIndex: 'updateBy',
        },
        {
          title: this.$t('操作'),
          dataIndex: 'action',
          align: 'center',
          fixed: 'right',
          width: 147,
          scopedSlots: { customRender: 'action' },
        },
      ],
      url: {
        list: '/game/bisGameTimerRecord/list',
        delete: '/game/bisGameTimerRecord/delete',
        deleteBatch: '/game/bisGameTimerRecord/deleteBatch',
        exportXlsUrl: '/game/bisGameTimerRecord/exportXls',
        importExcelUrl: 'game/bisGameTimerRecord/importExcel',
      },
      dictOptions: {},
      superFieldList: [],
      disabled: false,
    }
  },
  created() {
    this.getSuperFieldList()
  },
  computed: {
    importExcelUrl: function () {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
    },
  },
  methods: {
    initDictConfig() {},
    getSuperFieldList() {
      let fieldList = []
      fieldList.push({ type: 'datetime', value: 'beginTime', text: '开始时间' })
      fieldList.push({ type: 'string', value: 'gameId', text: '游乐项目', dictCode: 'bis_game,name,id' })
      fieldList.push({ type: 'string', value: 'deviceId', text: '设备编码', dictCode: 'bis_device_info,name,id' })
      fieldList.push({ type: 'string', value: 'memberPhone', text: '会员手机号', dictCode: '' })
      fieldList.push({ type: 'string', value: 'chipNo', text: '卡号', dictCode: '' })
      fieldList.push({ type: 'string', value: 'memberName', text: '姓名', dictCode: '' })
      fieldList.push({
        type: 'int',
        value: 'settleState',
        text: '结算状态',
        dictCode: 'bis_game_timer_record.settle_state',
      })
      fieldList.push({
        type: 'int',
        value: 'settleType',
        text: '结算方式',
        dictCode: 'bis_game_timer_record.settle_type',
      })
      fieldList.push({ type: 'datetime', value: 'endTime', text: '结束时间' })
      fieldList.push({ type: 'int', value: 'useTime', text: '消费时长（分钟）', dictCode: '' })
      fieldList.push({ type: 'BigDecimal', value: 'coin', text: '消费币值', dictCode: '' })
      fieldList.push({ type: 'string', value: 'settleDeviceId', text: '结算设备', dictCode: 'bis_device_info,name,id' })
      fieldList.push({ type: 'string', value: 'updateBy', text: '操作员', dictCode: 'sys_user,realname,realname' })
      this.superFieldList = fieldList
    },
    handleCheckout(record) {
      //结算
      let that = this
      if (that.disabled) {
        return
      }
      that.disabled = true
      that.$message.info(that.$t('开始结算'))
      getAction('/game/bisGameTimerRecord/prepareSettledTimer', {
        id: record.id,
      }).then((res) => {
        if (res.success) {
          if (res.result.records.length > 0) {
            let dtl = res.result.records[0]
            postAction('/trans/trans', {
              order: { typeId: 63 },
              gameTimerRecord: {
                id: dtl.id,
                endTime: dtl.endTime,
                coin: dtl.coin,
              },
            }).then((res1) => {
              if (res1.success) {
                that.disabled = false
                that.$message.success(res1.message)
                that.loadData(that.ipagination.current)
              } else if (res1.code == 504) {
                that.$confirm({
                  content: res1.message + that.$t('，强制结算将扣除卡上全部余额，您确定要执行结算吗？'),
                  okText: that.$t('强制结算'),
                  cancelText: that.$t('取消结算'),
                  onOk: function () {
                    postAction('/trans/trans', {
                      order: { typeId: 63, cont: true },
                      gameTimerRecord: {
                        id: dtl.id,
                        endTime: dtl.endTime,
                        coin: dtl.coin,
                      },
                    }).then((res2) => {
                      if (res2.success) {
                        that.disabled = false
                        that.$message.success(res2.message)
                        that.loadData(that.ipagination.current)
                      } else {
                        that.disabled = false
                        that.$message.error(res2.message)
                      }
                    })
                  },
                  onCancel() {
                  },
                })
              } else {
                that.disabled = false
                that.$message.error(res1.message)
              }
            })
          } else {
            that.disabled = false
          }
        } else {
          that.disabled = false
          that.$message.error(res.message)
        }
      })
    },
  },
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>